package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.commom.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "分类列表")
@Slf4j
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation("分类")
    // 分类列表

    @GetMapping("/category/findAll")
    public ResultInfo findAll() {
        List<Category> list = null;
        //先查询缓存
        String categoryKey = "category_list";

        if (redisTemplate.hasKey(categoryKey)) {
            // yes redis有数据
            list = (List<Category>) redisTemplate.opsForValue().get(categoryKey);
            log.info("查询Redis缓存");
        } else {
            // no redis没有数据
            // 调用service 查询mysql
            list = categoryService.findAll();
            log.info("查询MySQL数据库");
            //同步到Redis中
            redisTemplate.opsForValue().set(categoryKey, list);
        }
        // 返回结果
        return ResultInfo.success(list);
    }

    @ApiOperation("新增分类")
    // 新增分类
    @ApiImplicitParams({
            @ApiImplicitParam(name = "category", value = "分类对象", required = true),
    })
    @PostMapping("/category")
    public ResultInfo save(@RequestBody Category category) {// 1.接收参数
        // 2.调用service新增
        categoryService.save(category);
        //清理缓存
        redisTemplate.delete("category_list");
        log.info("Redis缓存已经清理");
        // 3.返回结果
        return ResultInfo.success();
    }
    @ApiOperation("修改分类")
    // 修改分类
    @ApiImplicitParams({
            @ApiImplicitParam(name = "category", value = "分类对象", required = true),
    })
    @PutMapping("/category")
    public ResultInfo update(@RequestBody Category category) {// 1.接收参数
        // 2.调用service修改
        categoryService.update(category);
        //清理缓存
        redisTemplate.delete("category_list");
        log.info("Redis缓存已经清理");
        // 3.返回结果
        return ResultInfo.success();
    }
    @ApiOperation("删除分类")
    // 删除分类
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "分类id", required = true),
    })
    @DeleteMapping("/category")
    public ResultInfo deleteById(Long id) {// 1.接收参数
        // 2.调用service删除
        categoryService.deleteById(id);
        //清理缓存
        redisTemplate.delete("category_list");
        log.info("Redis缓存已经清理");
        // 3.直接返回
        return ResultInfo.success();
    }
    @ApiOperation("查询分类")
    //根据type查询分类列表
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "分类类型 1 菜品分类 2 套餐分类", required = true),
    })
    @GetMapping("/category/list")
    public ResultInfo list(Integer type) {
        return ResultInfo.success(categoryService.list(type));
    }


}